# ------------------------------------------------------------------------------
# Makes targets for 07_limited_bandwidth_models/01_behavioral_lasso directory
# Author: Cassidy Shubatt <cshubatt@gmail.com>
# To run: bsub -q big -R "rusage[mem=150000]" make
# ------------------------------------------------------------------------------

# Config -----------------------------------------------------------------------
.ONESHELL:
include config.mk

# Recipes ----------------------------------------------------------------------
## all			: Produces all targets for LASSO limited bandwidth pipeline
.PHONY: all
all: performance_plot leftover_signal #feature_corr_plot

# Intermediate recipes
$(COHORT): ../../02_prep_and_summarize_cohort/
	$(MAKE) -C ../../02_prep_and_summarize_cohort/

## lassos			: Produces path-preserving LASSOS models for Y,T
.PHONY: lassos
lassos : $(LASSOS)
temp/lasso__%.rds : 01_fit_lasso.sh scripts/01_fit_lasso.R \
$(COHORT) $(DIRS)
	bash $< $*

$(UNPENALIZED_MOD) : 02_fit_unpenalized_along_path.sh \
scripts/02_fit_unpenalized_along_path.R temp/lasso__stent_or_cabg_010_day.rds
	bash $<

## scores			: Gets test-set scores for LASSO models, unpenalized T model
.PHONY: scores
scores : $(LASSO_SCORES) $(UNPENALIZED_SCORES)

temp/scores__lasso__%.rds: 03_predict_lasso.sh scripts/03_predict_lasso.R \
temp/lasso__%.rds
	bash $< $*

$(UNPENALIZED_SCORES): 04_predict_unpenalized_along_path.sh \
scripts/04_predict_unpenalized_along_path.R $(UNPENALIZED_MOD)
	bash $<

## performance		: Measures performance (AUC, R2) of LASSO models
.PHONY: performance
performance: $(PERFORMANCE)
$(PERFORMANCE): 05_measure_performance.sh scripts/05_measure_performance.R \
$(LASSO_SCORES) #$(UNPENALIZED_SCORES)
	bash $<

# Results recipes
## performance_plot	: Plots performance (AUC, R2) of LASSO to predict Y,T
.PHONY: performance_plot
performance_plot: $(PERFORMANCE_PLOT)
$(PERFORMANCE_PLOT): 06_plot_performance.sh scripts/06_plot_performance.R \
$(PERFORMANCE)
	bash $<

## feature_corr		: Gets correlation between physician feature wts and true wts
.PHONY: feature_corr
feature_corr: $(FEATURE_CORR)
$(FEATURE_CORR): 07_calc_feature_outcome_cor.sh \
scripts/07_calc_feature_outcome_cor.R $(PERFORMANCE)
	bash $<

## feature_corr_plot	: Plots correlation between physician/true feature wts
.PHONY: feature_corr_plot
feature_corr_plot: $(FEATURE_CORR_PLOT)
$(FEATURE_CORR_PLOT): 08_plot_feature_outcome_cor.sh \
scripts/08_plot_feature_outcome_cor.R $(FEATURE_CORR)
	bash $<

## leftover_signal	: Builds table for leftover signal regressions
.PHONY: leftover_signal
leftover_signal: $(LEFTOVER_SIGNAL)
$(LEFTOVER_SIGNAL): 09_leftover_signal.sh scripts/09_leftover_signal.R \
temp/scores__lasso__stent_or_cabg_010_day.rds
	bash $<

# Config targets ---------------------------------------------------------------
## dirs 			: Builds all directories.
.PHONY : dirs
dirs : $(DIRS)
%/.f :
	mkdir -p $(dir $@)
	touch $@

## style			: Styles R scripts according to tidyverse guidelines
.PHONY: style
style:
	source ~/anaconda3/etc/profile.d/conda.sh
	conda activate stressr
	R --vanilla --silent -e "styler::style_dir(path = 'scripts', filetype = c('R', 'Rmd', 'Rprofile'))"

## clean			: Deletes all output.
.PHONY : clean
clean :
	rm -rf temp

.PHONY : help
help : Makefile
	@sed -n 's/^##//p' $<
